package com.wtwd.wtwddevice.listener.deviceServerMsg.handler.impl;

import com.wtwd.device.annotation.MsgType;
import com.wtwd.device.constant.AnswerStateEnum;
import com.wtwd.device.constant.CmdStateEnum;
import com.wtwd.device.constant.MsgTypeEnum;
import com.wtwd.device.mqtentity.MessageBase;
import com.wtwd.device.protocol.AppProtocol;
import com.wtwd.device.protocol.WTProtocol;
import com.wtwd.wtwddevice.listener.deviceServerMsg.handler.IMsgHandler;
import com.wtwd.wtwddevice.service.IAppCommService;
import com.wtwd.wtwddevice.service.IDeviceService;
import com.wtwd.wtwddevice.service.ISetBloodPressureService;
import com.wtwd.wtwddevice.service.ISetHeartRateWarningService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 设置终端预警血压设备应答处理
 */
@MsgType(MsgTypeEnum.SET_BP_THRESHOLD)
@Component
public class SetBloodPressureWarning implements IMsgHandler {
    @Resource
    private IAppCommService appCommService;

    @Resource
    private IDeviceService deviceService;


    @Autowired
    private ISetBloodPressureService setBloodPressureService;

    @Override
    public void handleMsg(MessageBase messageBase) {
        WTProtocol wtProtocol = messageBase.getWtProtocol();
        Integer state = Integer.valueOf(wtProtocol.getBody());
        //应答数据推送给app
        if(state.equals(AnswerStateEnum.NORMAL.getState())){
            Long deviceId = deviceService.getDeviceIdByImei(wtProtocol.getImei());
            int i = setBloodPressureService.editState(deviceId, wtProtocol.getNumber(), CmdStateEnum.SUCCESS.getState());
            Integer recoverState = AnswerStateEnum.ABNORMAL.getState();
            if(i == 1){
                recoverState = AnswerStateEnum.NORMAL.getState();
            }

            AppProtocol appProtocol = AppProtocol.builder()
                    .imei(wtProtocol.getImei())
                    .cmdName(MsgTypeEnum.APP_SET_BLOOD_PRESSURE_WARNING.getCmdName())
                    .data(null)
                    .state(recoverState)
                    .build();
            appCommService.pushDeviceState(deviceId, appProtocol);
        }
    }
}
